package action;

import interfaces.HttpHeader;

import java.io.IOException;
import java.io.Writer;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;
import utils.CommonUtil;

import com.xiaoleilu.hutool.FileUtil;
import com.xiaoleilu.hutool.Log;
import com.xiaoleilu.hutool.StrUtil;

import bean.Request;
import bean.Response;

public class Controller {
	protected Request req;
	protected Response resp;
	protected Writer writer;
	protected String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
	
	public Controller(){
		
	}
	public void excute(Request req,Response resp,String method, Class clazz){
		this.req = req;
		this.resp = resp;
		this.writer = resp.getWriter();
		method = method.replace("/", "");
		if(StrUtil.isEmpty(method)){
			try {
				Method mt = clazz.getMethod("index", null);
				mt.invoke(this, null);
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
		}else{
			try {
				Method mt = clazz.getMethod(method, null);
				mt.invoke(this, null);
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				
				e.printStackTrace();
			} catch (InvocationTargetException e) {
			
				e.printStackTrace();
			}
		}
	}
	protected void index(){
		Log.debug("do nothing");
	}
	protected void renderJson(Object o){
		JSONArray bodyJa = null;
		JSONObject bodyJo = null;
		String body = "";
		if(o instanceof List || o instanceof Object[]){
			bodyJa = JSONArray.fromObject(o);
			body = bodyJa.toString();
		}else if(o instanceof Map || o instanceof String){
			bodyJo = JSONObject.fromObject(o);
			body = bodyJo.toString();
		}else{
			Log.error("对象转换为Json数据出错");
		}
		Log.debug("格式化JSON数据body={}", body);
		resp.setHttpVersion("HTTP/1.1");
		resp.setStatusCode("200");
		resp.setStatus("OK");
		resp.setHeader(HttpHeader.DATE, new Date().toString());
		resp.setHeader(HttpHeader.CONTENT_TYPE, "application/json;charset=UTF-8");
		resp.setHeader(HttpHeader.CONTENT_LENGTH, body.getBytes().length);
		try {
			writer.write(CommonUtil.resp2Str(resp));
			writer.write(body);
			writer.flush();
			writer.close();
			Log.debug("发送JSON成功");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	protected void renderHTML(String htmlpath){
		try {
			String body = FileUtil.readString(path+htmlpath,"UTF-8");
			
			resp.setHttpVersion("HTTP/1.1");
			resp.setStatusCode("200");
			resp.setStatus("OK");
			resp.setHeader(HttpHeader.DATE, new Date().toString());
			resp.setHeader(HttpHeader.CONTENT_TYPE, "text/html;charset=UTF-8");
			resp.setHeader(HttpHeader.CONTENT_LENGTH, body.getBytes().length);
			resp.setBody(body);
			
			writer.write(CommonUtil.resp2Str(resp));
			writer.flush();
			writer.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
